package com.pocketpets.main;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.app.pocketpets.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class OldMap extends MapActivity implements LocationListener {

	// ================================================================
	// Fields
	// ================================================================

	private MapView mapView;
	private MapController mapController;
	private LocationManager mLocationManager;
	private MyLocationOverlay mLocationOverlay;
	private List<Overlay> mapOverlays;
	private MyItemizedOverlay mMyItemizedOverlay;

	// ================================================================
	// Inheried methods
	// ================================================================

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.searchmap);
		// Create Google Map View
		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		mapController = mapView.getController();
		mapController.setZoom(18);

		// Add item to the Google Map

		initMap();

		Drawable drawable = this.getResources().getDrawable(
				R.drawable.pet_silver);
		UselessItemizedOverlay itemizedoverlay = new UselessItemizedOverlay(drawable, this);
		Drawable drawable2 = this.getResources().getDrawable(
				R.drawable.pet_gold);
		mMyItemizedOverlay = new MyItemizedOverlay(drawable2, mapView);

		GeoPoint point = new GeoPoint(22336004, 114263971);
		OverlayItem overlayitem = new OverlayItem(point, "Helo, Master!",
				"I'm a Lv 10 Silver Pets!");
		GeoPoint point2 = new GeoPoint(22337513, 114263005);
		OverlayItem overlayitem2 = new OverlayItem(point2, "Helo, Master!",
				"I'm a Lv 1 Gold Pets!");
		// overlayitem2.setMarker(drawable2);

		mMyItemizedOverlay.addOverlay(overlayitem);
		mMyItemizedOverlay.addOverlay(overlayitem2);
		mapOverlays.add(mMyItemizedOverlay);
		// itemizedoverlay.addOverlay(overlayitem);
		// itemizedoverlay.addOverlay(overlayitem2);
		// mapOverlays.add(itemizedoverlay);

		// mapOverlays.add(itemizedoverlay);
		// mapOverlays.add(itemizedoverlay2);
		// MyLocationOverlay myLocation=new myLocation();

		// mapView.mo
		mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
				10, 0, this);

		// geo fix 114.263949 22.336600
		// geo fix 114.263949 21.336600
	}

	public void onLocationChanged(Location location) {
		if (location != null) {
			int lat = (int) (location.getLatitude() * 1E6);
			int lng = (int) (location.getLongitude() * 1E6);
			GeoPoint point = new GeoPoint(lat, lng);
			mapController.animateTo(point);

		}
	}

	public void onProviderDisabled(String provider) {
	}

	public void onProviderEnabled(String provider) {
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
	}

	private void initMap() {
		mLocationOverlay = new MyLocationOverlay(this, mapView);
		mapOverlays = mapView.getOverlays();
		mapOverlays.add(mLocationOverlay);
		mLocationOverlay.enableCompass();
		mLocationOverlay.runOnFirstFix(new Runnable() {
			public void run() {
				mapController.animateTo(mLocationOverlay.getMyLocation());
			}
		});
	}

	@Override
	protected void onResume() {

		// TODO Auto-generated method stub
		super.onResume();
		mLocationOverlay.enableMyLocation();

	}

	@Override
	protected void onPause() {
		super.onPause();
		mLocationOverlay.disableMyLocation();
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

}
